﻿@using RMS.EF.Model
@model List<RMS_Role>
@{
    var id = ViewBag.Id as int?;
    List<string> checkRoles = ViewBag.RolesId;
    var errorMsg = ViewBag.ErrorMsg as string;
    if (!string.IsNullOrWhiteSpace(errorMsg))
    {
        <script>
            toastr.error("@(errorMsg)");
        </script>
    }
}

<div class="form-horizontal">

        @Html.Hidden("id", id)
        @Html.Hidden("roleId")

    <ul class="list-group">
        @for (int i = 0; i < Model.Count; i++)
        {
            var checkstr = string.Empty;
            if (checkRoles.Contains(Model[i].Id))
            {
                checkstr = "checked=\"checked\"";
            }
            
            <li class="list-group-item">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="role" value="@Model[i].Id" @checkstr > @Model[i].Name
                    </label>
                </div>
            </li>
        }
    </ul>
</div>
<script>
    $(function () {
        $("#btnSave").unbind();
        $("#modal-content").unbind();
        $("#btnSave").click(function () {
            save();
        });
        $("#modal-content").submit(function () {
            save();
            return false;
        });
    });
    function save() {
        var roleId = [];
        $("input:checkbox[name='role']:checked").each(function () {
            roleId.push($(this).val());
        });
        $("#roleId").val(roleId);
        $("#modal-content").formSave({ search: function () { table.ajax.reload(null, false); } });
    }
</script>